<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	/*******main*******/
	//团详情
	$orderid = intval($_RQ['orderid']);
	$teamid = intval($_RQ['teamid']);
	if(!$orderid && !$teamid){
		exi('该团不存在!','error');
	}
	$team=pdo_get('jy_paiweifee_team',array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	$myorder=pdo_get('jy_paiweifee_team_log',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'team_id'=>$teamid));
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['goodsid']));
	$team['member']=cfc('core')->member_show($team['mid']);
	$team['isnum']=pdo_count('jy_paiweifee_team_log',array('uniacid'=>$_SESSION['uniacid'],'team_id'=>$team['id']));
	$team['teamnum']=$this->model('team')->getteamnum($team);
	$team['pai_fee']=$this->setting['pai_fee']*$goods['price']/100;
	$team['pai_tuifee']=$this->setting['pai_tuifee']*$goods['price']/100;
	//商品拼团价
	$n=$team['teamnum']-$team['isnum'];
	//商品多图
	$goods['images']=$goods['images']?json_decode($goods['images'],true):array($goods['image']);
	//查询该团的所有人
	$paiweilogs=$this->model('team')->getpaiweilogs($team);
	//限时时间
	$hours = $goods['grouptime']?:10;
	$endtime = $hours*60+$team['createtime'];
	$lasttime2 = $endtime-time();
	if($team['status']==0){
		$team['statustitle']='快邀请好友一起来拼团吧！';
	}elseif($team['status']==1){
		if($myorder['status']!=1 && $myorder['status']!=2 && $myorder['status']!=3){
			$team['statustitle']=$this->order_status[$myorder['status']];
		}else{
			$team['statustitle']='团购已成功！';
		}
	}else{
		$team['statustitle']='团购已结束！';
	}
	$return['team']=$team;
	$return['paiweilogs']=$paiweilogs;
	$alllogs=pdo_getall('jy_paiweifee_team_log',array('team_id'=>$team['id']),'','mid');
	if($alllogs){
			foreach($alllogs as &$item){
						$item['member']=cfc('core')->member_show($item['mid']);
						$item['member']['avatar']=$item['member']['avatar']?tomedia($item['member']['avatar']):'';
						if($item['mid']==$team['mid']){
							$nickname=$item['member']['nickname'];
						}
						$item['createtime']=date('Y-m-d h:i',$item['createtime']);
			}
	}
	$return['alllogs']=$alllogs;
	$return['myorder']=$myorder?:array();
	$return['n']=$n;
	//加载商品供参团，商品规格等
	//默认已选参数：
	$store_id=$this->session('store_id');
	$goods['canbuy'] = ($goods['status'] == 1) && empty($goods['deleted']) && ($goods['total'] > 0);
	if (0 < $goods['usermaxbuy']) {
		$order_goodscount = pdo_fetchcolumn('select ifnull(sum(og.total),0)  from ' . tablename('jy_paiweifee_team_log_goods') . ' og ' . ' left join ' . tablename('jy_paiweifee_team_log') . ' o on og.orderid=o.id ' . ' where og.goodsid=:goodsid and  o.status>=1 and o.mid=:mid  and og.uniacid=:uniacid ', array(':goodsid' => $goods['id'], ':uniacid' => $_SESSION['uniacid'], ':mid' => $_SESSION['uid']));
		$goods['order_goodscount']=$order_goodscount;
		if ($goods['usermaxbuy'] <= $order_goodscount) {
			$goods['userbuy'] = 0;
			$goods['canbuy'] = false;
		}
	}
	if ((0 < $goods['isendtime']) && (0 < $goods['endtime']) && ($goods['endtime'] < time())) {
		$goods['canbuy'] = false;
	}
	$pindex = max(1, 1);
	$parameter=array(
					'params'=>array('goodsid'=>$goods['id']),
					'limit'=>array($pindex,1),
					'fields'=>$fields,
					'orderby'=>'price asc',
	);
	$optionsone = pdo_getlist('jy_weishop_goods_option',$parameter);
	if($optionsone){
		$optionsid=$optionsone[0]['id'];
		$spsids=$optionsone[0]['specs'];
		//规格
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
		//已选规格
		$selectspecs = explode('_',$spsids);
		$selespename = '';
		if($specs_list){
				foreach($selectspecs as $spv){
					$spsitem=pdo_get('jy_weishop_goods_spec_item',array('id'=>$spv,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($spsitem['title']){
						$selespename .=$spsitem['title'].'，';
					}
				}
				foreach($specs_list as $k=>&$sp){
					$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				}
				if(count($specs_list)>0){
					$goods['optiontotal']=pdo_getsum('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goods['id']),'stock');
					if($goods['optiontotal']<0){
						$goods['canbuy']==false;
					}else{
						$goods['canbuy']==true;
					}
				}
		}
	}
	//默认商品规格价格：
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods= model('jy_weishop/activity')->get_upgoods($goods);
	$return['options']=$goods['options']?:array();
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	$goods['image']=$return['options']['thumb']?:$goods['image'];
	$return['selespename'] = $selespename;
	$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goods['id']),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
	if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			//$specs_list['option']=$option_list;
	}
	$return['specs_list'] = $specs_list;
	//判断nobuy状态：
	if($goods['userbuy']==0){
		$goods['nobuytitle']='您已经超出最大'.$goods['usermaxbuy'].'件购买量';
	}elseif($goods['levelbuy']==0){
		$goods['nobuytitle']='您当前会员等级没有购买权限';
	}elseif($goods['groupbuy']==0){
		$goods['nobuytitle']='您所在的用户组没有购买权限';
	}elseif($goods['timebuy']==-1){
		$goods['nobuytitle']='未到开始抢购时间!';
	}elseif($goods['timebuy']==1){
		$goods['nobuytitle']='抢购时间已经结束';
	}else{
		$goods['nobuytitle']='商品已售罄';
	}
	//POST提交：
	$post = array();
	if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		$post['isdish'] = true;
	}
	$post['num'] = $this->model('team')->get_dangnum($team['mid']);
	$post['goodsid'] = $goods['id'];
	$post['specid']=$selectspecs;
	$post['optionsid']=$return['options']['id']?:0;
	$post['storeid']=0;
	$post['teamid']=$team['id'];
	$return['post']=$post;
	if(!$return['selespename']){
		$return['gobuy']=1;
	}
	if($return['selespename']){
		$return['gobuy']=2;
	}
	$return['goods']=$goods;
	$return['sharedata']=array(
			'title'=>$nickname.'-发起拼单啦！还差'.$n.'人就拼单成功了！',
			'desc'=>'拼着买，更便宜，'.$goods['title'].'-拼单特价啦！',
			'link'=>custom_url('jy_paiweifee/teamdetail//teamid='.$teamid.'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($goods['image'])
	);
	set_html('title',$nickname.'-拼单详情');
	exi($return);
	
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	$store_id=$this->session('store_id');
	if(!$id){
		exi('请先选择商品！','error');
	}
	if($post['storeid']){
		
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$optionsid=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']),'id');
	//默认商品规格价格：
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods= model('jy_weishop/activity')->get_upgoods($goods);
	$return['options']=$goods['options'];
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	if($post['isdish']){
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}else{
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='check_buy'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	model('jy_weishop/goods')->check_buy($goodsid,$_RQ['num'],$_RQ['optionsid']);
	$this->model('team')->checkteam($_RQ['teamid']);
	exi('','','jy_weishop/order.create//plugin_name=jy_paiweifee&teamid='.$_RQ['teamid'].'&goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
}
?>